<?php
/**
 * 今天值得买首页
 * @author wubaiqing<wubaiqing@vip.qq.com>
 * @copyright Copyright (c) 2013 今天值得买
 * @since 1.5
 */
class SiteController extends Controller
{

    /**
     * @var string $layout 继承视图
     */
    public $layout = '//layouts/main';

    /**
     * 页面访问权限
     * @return array 权限列表
     */
    public function accessRules()
    {
        return array(
            array('allow',
                'actions' => array('downurl'),
                'users'=>array('*'),
            )
        );
    }

    /**
     * 今天值得买首页
     * @param integer $cat  当前分类
     * @param intger  $page 当前页数
     * @param integer $hot  0最热, 1热门
     */
    public function actionMobile()
    {
        $this->render('mobile', array(
        ));
    }

    /**
     * 今天值得买首页
     * @param integer $cat  当前分类
     * @param intger  $page 当前页数
     * @param integer $hot  0最热, 1热门
     */
    public function actionIndex($cat = 0, $page = 1, $hot = 0)
    {
        $goodsList = Goods::getGoodsList($page, $cat, $hot);

        $this->render('index', array(
            'hot' => $hot,
            'catId' => $cat,
            'data' => $goodsList['data'],
            'pager' => $goodsList['pager'],
        ));
    }

    /**
     * 逛
     */
    public function actionGuang($page = 1, $tagId = 0)
    {
        $guangList = Guang::getGuangList($tagId, $page);

        $this->render('guang', array(
            'tagId' => $tagId,
            'data' => $guangList['data'],
            'pager' => $guangList['pager']
        ));

    }

    /**
     * 下载桌面链接
     */
    public function actionDownUrl()
    {
        $Shortcut="
            [InternetShortcut]
            URL=http://www.40zhe.com/
            IconFile=http://www.40zhe.com/static/images/favicon.ico
            IconIndex=1
            IDList=
            [{000214A0-0000-0000-C000-000000000046}]
            Prop3=19,2
        ";
        Header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=今天值得买.url;");
        echo $Shortcut;
    }

    /**
     * 今天值得买商品详情页
     * @param integer $goodsId 商品ID
     */
    public function actionDetails($goodsId)
    {
        $banner = Banner::getAll();
        $getCate = Cat::getAllCat();
        $goods = Goods::getGoods($goodsId);

        $this->pageTitle = $goods->title;

        $getGoodsCate = Goods::getGoodsCateId($goods->cat_id);
        $getGuangCate = Guang::getGuangCateId($goods->id);

        if (empty($goods)) {
            $this->render('site/index');
        }

        $this->render('details', array(
            'banner' => $banner,
            'cat' => $getCate,
            'goods' => $goods,
            'goodsCate' => $getGoodsCate,
            'guangCate' => $getGuangCate,
        ));
    }

    public function actionGetGold()
    {
        $alipayId = $error = '';
        $this->render('gold', array(
            'alipayId' => $alipayId,
            'error' => $error
        ));
    }

	public function returnData($status, $data)
	{
		echo json_encode(array('status' => $status, 'data' => $data));
		Yii::app()->end();
	}
	

    public function actionPostData($alipayId, $qq)
    {
        $alipayId = trim(addslashes($alipayId));
        $qq = trim(addslashes($qq));
        $ip = Yii::app()->request->userHostAddress;

        if (preg_match("/^[0-9a-zA-Z\.]+@(([0-9a-zA-Z]+)[.])+[a-z]{2,4}$/i", $alipayId) || strlen($alipayId) == 11) {
			if (preg_match('/[^\d]/', $qq) || empty($qq)) {
				$this->returnData('0', 'QQ号格式错误，只能是数字QQ号');
			}

            $goldQQ = Gold::model()->count(array(
                'condition' => 'qq =:qq',
                'params' => array(':qq' => $qq)
            ));

			if ($goldQQ) {
				$this->returnData('0', '您的QQ已被添加，不能重复领取，集分宝');
			}
            $goldIp = Gold::model()->count(array(
                'condition' => 'ip =:ip',
                'params' => array(':ip' => $ip)
            ));
            if ($goldIp >= 5) {
				$this->returnData('0', '您最多只能添加5个支付宝账号');
            } else {
                $gold = Gold::model()->findAll(array(
                    'condition' => 'alipay =:alipay',
                    'params' => array(':alipay' => $alipayId)
                ));

                if (empty($gold)) {
                    $gold = new Gold();
                    $gold->alipay = $alipayId;
                    $gold->ip = $ip;
                    $gold->status = 0;
                    $gold->qq = $qq;
                    $gold->save();
					$this->returnData('1', '提交成功！请明天登陆您的支付宝账号查收。');
                } else {
					$this->returnData('0', '该账户已被添加');
                }
            }
        } else {
			$this->returnData('0', '支付宝账号格式不正确，请输入邮箱/手机账号');
        }

    }
}
